<template>
  <ContentWrap>
    <div class="flex justify-end mb-4">
      <el-button type="primary" @click="handleAdd">新增</el-button>
    </div>
    <el-table
      :data="tableData"
      v-loading="loading"
      style="width: 100%"
      :stripe="true"
      :show-overflow-tooltip="true"
    >
      <el-table-column label="套餐类型名称" prop="name" align="center" />
      <el-table-column label="套餐类型描述" prop="description" align="center" />
      <el-table-column label="操作" prop="createBy" align="center">
        <template #default="scope">
          <el-button type="primary" size="small" link @click="handleEdit(scope.row)"
            >编辑</el-button
          >
          <el-button type="primary" size="small" link @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      :total="total"
      @pagination="getPackTypePage"
    />
    <AddType
      ref="addTypeRef"
      v-if="isViewType"
      @close="isViewType = false"
      :editInfo="editInfo"
      @submit="handleSubmit"
    />
  </ContentWrap>
</template>
<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import AddType from './addType.vue'
import { SofaBasicApi } from '@/api/application/sofa'

const tableData = ref([])
const loading = ref(false)
const isViewType = ref(false)
const total = ref(0)
const queryParams = ref({
  pageNo: 1,
  pageSize: 10
})
const message = useMessage()
const editInfo = ref<any>({})
const handleAdd = () => {
  editInfo.value = {}
  isViewType.value = true
}
const handleEdit = (row: any) => {
  editInfo.value = row
  isViewType.value = true
}
const handleDelete = (row: any) => {
  console.log(row)
  ElMessageBox.confirm('确定删除该套餐类型吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    await SofaBasicApi.deletePackageType({ id: row.id })
    message.success('删除成功')
    getPackTypePage()
  })
}
const handleSubmit = async (data: any) => {
  console.log(data)
  if (editInfo.value.id) {
    await SofaBasicApi.updatePackageType(data)
    message.success('更新成功')
  } else {
    await SofaBasicApi.createPackageType(data)
    message.success('创建成功')
  }
  isViewType.value = false
  getPackTypePage()
}
const getPackTypePage = async () => {
  loading.value = true
  try {
    const res = await SofaBasicApi.getPackageTypePage(queryParams.value)
    tableData.value = res.list
    total.value = res.total
  } catch (error) {
    console.log(error)
  } finally {
    loading.value = false
  }
}
onMounted(() => {
  getPackTypePage()
})
</script>
<style lang="scss" scoped></style>
